//go:build windows
// +build windows

/*
 * @Author: Wangjun
 * @Date: 2023-03-17 13:23:03
 * @LastEditTime: 2025-05-21 13:26:05
 * @LastEditors: wangjun haodreams@163.com
 * @Description:
 * @FilePath: \libs\utils\utils_windows.go
 * hnxr
 */

package utils

import (
	"os/exec"
	"syscall"
)

const (
	WM_SHOWWINDOW = 24

	SW_HIDE = 0
	SW_SHOW = 5
)

// 导入Windows API函数
var (
	user32               = syscall.NewLazyDLL("user32.dll")
	kernel32             = syscall.NewLazyDLL("kernel32.dll")
	postMessage          = user32.NewProc("PostMessageW")
	procGetConsoleWindow = kernel32.NewProc("GetConsoleWindow")
)

func PostMessage(hWnd uintptr, msg uint32, wParam, lParam uintptr) uintptr {
	ret, _, _ := syscall.SyscallN(postMessage.Addr(), uintptr(hWnd), uintptr(msg), wParam, lParam)
	return ret
}

// 获取控制台窗口句柄
func GetConsoleWindow() uintptr {
	hwnd, _, _ := procGetConsoleWindow.Call()
	return hwnd
}

// ShowWindow 显示窗口
func ShowWindow(show bool) {
	hwnd := GetConsoleWindow()

	if show {
		PostMessage(hwnd, WM_SHOWWINDOW, SW_SHOW, 0)
	} else {
		PostMessage(hwnd, WM_SHOWWINDOW, SW_HIDE, 0)
	}
}

// Reboot 重启系统
func Reboot() (err error) {
	cmd := exec.Command("shutdown", "-r", "-t", "10")
	return cmd.Start()
}
